//
//  Choice.m
//  PTravel
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "Choice.h"

@implementation Choice

- (void)dealloc
{
    [_myTableView release];
    [_mySmallTableView release];
    [_smallArr release];
    [_typeArr release];
    [_depart release];
    [_destinationArr release];
    [_timeArr release];
    [_countryArr release];
    [_bigView release];
    [_city release];
    [_timeStr release];
    [_depart release];
    [_type release];
    [_destination release];
    [_time release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changColor) name:@"night" object:nil];
        [self createSubViews];
    }
    return self;
}

- (void)changColor
{
    self.bigView.backgroundColor = CELLCOLOR;
    self.backgroundColor = CELLCOLOR;
    self.mySmallTableView.backgroundColor = CELLCOLOR;
    self.myTableView.backgroundColor = CELLCOLOR;
    [self.myTableView reloadData];
    [self.mySmallTableView reloadData];
    [self.type setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.depart setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.time setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.destination setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
}

- (void)createSubViews
{
    
    self.backgroundColor = CELLCOLOR;
    UIView *aview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS.size.width, 0.5)];
    aview.backgroundColor = [UIColor colorWithWhite:0.328 alpha:1.000];
    [self addSubview:aview];
    [aview release];
    
    self.type = [UIButton buttonWithType:UIButtonTypeCustom];
    self.type.frame = CGRectMake(0, 0, BOUNDS.size.width/4, 40);
    [self.type setTitle:@"全部类型" forState:UIControlStateNormal];
    [self.type setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.type addTarget:self action:@selector(typeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.type];
    
    
    self.depart = [UIButton buttonWithType:UIButtonTypeCustom];
    self.depart.frame = CGRectMake(BOUNDS.size.width/4, 0, BOUNDS.size.width/4, 40);
    [self.depart setTitle:@"出发地" forState:UIControlStateNormal];
    [self.depart setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.depart addTarget:self action:@selector(departAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.depart];
    
    self.destination = [UIButton buttonWithType:UIButtonTypeCustom];
    self.destination.frame = CGRectMake(BOUNDS.size.width/4*2, 0, BOUNDS.size.width/4, 40);
    [self.destination setTitle:@"目的地" forState:UIControlStateNormal];
    [self.destination setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.destination addTarget:self action:@selector(destinationAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.destination];
    
    self.time = [UIButton buttonWithType:UIButtonTypeCustom];
    self.time.frame = CGRectMake(BOUNDS.size.width/4*3, 0, BOUNDS.size.width/4, 40);
    [self.time setTitle:@"旅行时间" forState:UIControlStateNormal];
    [self.time setTitleColor:TEXTCOLOR forState:UIControlStateNormal];
    [self.time addTarget:self action:@selector(timeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.time];
    
    UIView *bview = [[UIView alloc] initWithFrame:CGRectMake(BOUNDS.size.width/4, 0, 0.5, 40)];
    bview.backgroundColor = [UIColor colorWithWhite:0.328 alpha:1.000];
    [self addSubview:bview];
    [bview release];
    
    UIView *cview = [[UIView alloc] initWithFrame:CGRectMake(BOUNDS.size.width/4*2, 0, 0.5, 40)];
    cview.backgroundColor = [UIColor colorWithWhite:0.328 alpha:1.000];
    [self addSubview:cview];
    [cview release];
    
    UIView *dview = [[UIView alloc] initWithFrame:CGRectMake(BOUNDS.size.width/4*3, 0, 0.5, 40)];
    dview.backgroundColor = [UIColor colorWithWhite:0.328 alpha:1.000];
    [self addSubview:dview];
    [dview release];
    
    UIView *zview = [[UIView alloc] initWithFrame:CGRectMake(0, 39, BOUNDS.size.width, 0.5)];
    zview.backgroundColor = [UIColor colorWithWhite:0.328 alpha:1.000];
    [self addSubview:zview];
    [zview release];
    
    
    
#pragma mark ===  大的tableView
    self.bigView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, BOUNDS.size.width, 0)];
    self.bigView.backgroundColor = CELLCOLOR;
    [self addSubview:self.bigView];
    
    self.typeArr = [NSMutableArray array];
    self.departArr = [NSMutableArray array];
    self.destinationArr = [NSMutableArray array];
    self.timeArr = [NSMutableArray array];
    [self getTopData];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, BOUNDS.size.width/2, 0) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.backgroundColor = CELLCOLOR;
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.bigView addSubview:self.myTableView];
    [_myTableView release];
    
    
#pragma mark === 小的tableView
    self.isDes = NO;
    self.smallArr = [NSMutableArray array];
    self.mySmallTableView = [[UITableView alloc] initWithFrame:CGRectMake(BOUNDS.size.width/2, 0, BOUNDS.size.width/2, 0) style:UITableViewStylePlain];
    self.mySmallTableView.tag = 1100;
    self.mySmallTableView.delegate = self;
    self.mySmallTableView.dataSource = self;
    self.mySmallTableView.backgroundColor = CELLCOLOR;
    [self.mySmallTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell1"];
    [self.bigView addSubview:self.mySmallTableView];
    [_mySmallTableView release];
    [self.mySmallTableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
}

#pragma mark === top数据请求
- (void)getTopData
{
    [AppTools getAFN:@"http://open.qyer.com/lastminute/get_all_categorys?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&departure=&lat=38.88255639279708&lon=121.5396443687879&oauth_token=7a1da33fff703bde846a4d6980671677&page=1&times=&track_app_channel=App%2520Store&track_app_version=6.8&track_device_info=iPhone7%2C2&track_deviceid=94E2CF74-5FF7-49B9-8D69-620BF45F65A9&track_os=ios%25209.1&track_user_id=7017902&type=0&v=1" block:^(id result) {
        NSMutableDictionary *bigDic = result;
        NSMutableDictionary *smallDic = [bigDic objectForKey:@"data"];
        self.typeArr = [smallDic objectForKey:@"type"];
        self.departArr = [smallDic objectForKey:@"departure"];
        self.destinationArr = [smallDic objectForKey:@"poi"];
        self.timeArr = [smallDic objectForKey:@"times_drange"];
        

        
        [self.myTableView reloadData];
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag == 1100) {
       
        if (self.isDes == YES) {
            NSLog(@"smallArr.count:%ld", self.smallArr.count);
            return self.smallArr.count;
        }else{
           
            return 0;
        }
    }else{
        switch (self.num) {
            case 0:
                return self.typeArr.count;
                break;
            case 1:
                return self.departArr.count;
                break;
            case 2:
                return self.destinationArr.count;
                break;
            case 3:
                return self.timeArr.count;
                break;
            default:
                return 0;
                break;
        }

    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.mySmallTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.textLabel.text = [[self.smallArr objectAtIndex:indexPath.row] objectForKey:@"country_name"];
        cell.textLabel.textColor = TEXTCOLOR;
        cell.backgroundColor = CELLCOLOR;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.backgroundColor = CELLCOLOR;
        cell.textLabel.textColor = TEXTCOLOR;
        switch (self.num) {
            case 0:
                cell.textLabel.text = [[self.typeArr objectAtIndex:indexPath.row] objectForKey:@"catename"];
                return cell;
                break;
            case 1:
                cell.textLabel.text = [[self.departArr objectAtIndex:indexPath.row] objectForKey:@"city_des"];
                return cell;
                break;
            case 2:
                cell.textLabel.text = [[self.destinationArr objectAtIndex:indexPath.row] objectForKey:@"continent_name"];
                return cell;
                break;
            case 3:
                cell.textLabel.text = [[self.timeArr objectAtIndex:indexPath.row] objectForKey:@"description"];
                return cell;
                break;
            default:
                return cell;
                break;
        }

    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1100) {
        //self.smallArr = [self.destinationArr objectAtIndex:indexPath.row];
        self.country_id = [[self.smallArr objectAtIndex:indexPath.row] objectForKey:@"country_id"];
        [self.choiceDelegate cellAction];
    }else{
        
        switch (self.num) {
            case 0:
                self.id = [[self.typeArr objectAtIndex:indexPath.row] objectForKey:@"id"];
                [self.choiceDelegate cellAction];
                break;
            case 1:
                self.departStr = [[self.departArr objectAtIndex:indexPath.row] objectForKey:@"city"];
                [self.choiceDelegate cellAction];
                break;
            case 2:
                self.continent_id = [[self.destinationArr objectAtIndex:indexPath.row] objectForKey:@"continent_id"];
                self.smallArr = [[self.destinationArr objectAtIndex:indexPath.row] objectForKey:@"country"];
                [self.mySmallTableView reloadData];
                break;
            case 3:
                self.timeStr = [[self.timeArr objectAtIndex:indexPath.row] objectForKey:@"times"];
                [self.choiceDelegate cellAction];
                break;
            default:
                
                break;
        }
    }
    
    

}

#pragma mark === 四个按钮

- (void)typeAction:(UIButton *)btn
{
    NSLog(@"类型");
    
    self.num = 0;
    [self.choiceDelegate type:(UIButton *)btn];
    [self.myTableView reloadData];
    [self.mySmallTableView reloadData];
}

- (void)departAction:(UIButton *)btn
{
    self.num = 1;
    NSLog(@"出发地");
    [self.choiceDelegate depart:(UIButton *)btn];
    [self.myTableView reloadData];
        [self.mySmallTableView reloadData];
}

- (void)destinationAction:(UIButton *)btn
{
        NSLog(@"目的地");
    self.num = 2;
    [self.choiceDelegate destination:(UIButton *)btn];
    [self.myTableView reloadData];
        [self.mySmallTableView reloadData];
}

- (void)timeAction:(UIButton *)btn
{
    NSLog(@"旅行时间");
    self.num = 3;
    [self.choiceDelegate time:(UIButton *)btn];
    [self.myTableView reloadData];
        [self.mySmallTableView reloadData];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
